package com.ccl.iot.device;

import com.ccl.iot.object.IOTDeviceInfoEx;

public class IOTComplexDevice extends IOTDevice {
    protected IOTDevice iDevices[] = null;

    public IOTComplexDevice(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aRegAddress, int aRegSize, IOTDevice... aDevices) {
        super(aDevID, aDeviceInfo, aRegAddress, aRegSize);

        iDevices = aDevices;

        if (iDevices != null) {
            final int length = iDevices.length;
            for (int i = 0; i < length; i++) {
                iDevices[i].setHideable(true);
                iDevices[i].setOnHideRestoreListener(new OnHideRestoreListener() {

                    @Override
                    public void onRestoreStateChange(boolean restore) {

                    }

                    @Override
                    public void onHideStateChange(boolean hide) {
                        int hideCount = 0;
                        //						int hideIndex = -1;
                        int displayCount = 0;
                        int displayIndex = -1;
                        for (int i = 0; i < length; i++) {
                            iDevices[i].setHideable(true);
                            iDevices[i].setRestoreable(hide);

                            if (iDevices[i].isHide()) {
                                hideCount++;
                                //								hideIndex = i;
                            } else {
                                if (iDevices[i].isOnline()) {
                                    displayCount++;
                                    displayIndex = i;
                                    iDevices[i].setHideable(true);
                                }
                            }
                        }

                        if (displayCount == 1) {
                            iDevices[displayIndex].setHideable(false);
                            iDevices[displayIndex].setRestoreable(hideCount == 0 ? false : true);
                        }

                    }

                    @Override
                    public void retoreOuther() {
                        for (int i = 0; i < length; i++) {
                            iDevices[i].setHide(false);
                        }
                    }
                });
            }
        }
    }

    @Override
    public void setRegisterInterface(IOTRegisterInterface aInterface) {
        super.setRegisterInterface(aInterface);

        if (iDevices != null) {
            int length = iDevices.length;
            for (int i = 0; i < length; i++) {
                iDevices[i].setRegisterInterface(aInterface);
            }
        }
    }

    @Override
    public byte[] getRegister() {
        return null;
    }

    @Override
    public void updateRegister(int aAddress, int aSize, byte[] aData, int aDataPos, int aNetWork, long time) {
        saveRegister(aAddress, aSize, aData, aNetWork);
        if (iDevices != null) {
            int address, size;
            int tStartPos = 0;
            /*for (int i = 0; i < iDevices.length; i++){
                if (iDevices[i] != null){
					boolean tMatch = true;
					
					address = iDevices[i].getRegisterAddress();
					size = iDevices[i].getRegisterSize();
					
					if (aAddress <= address){
						tStartPos = (address - aAddress);
						
						if (aSize - tStartPos < size){
							tMatch = false;
						}
					}else{
						tMatch = false;
					}
					
					if (tMatch){
						iDevices[i].updateRegister(address, size, aData, tStartPos);
					}
					
					
				}*/


            //====================modify start==================================


            int aRegLen = aAddress + aSize;
            int length = iDevices.length;
            for (int i = 0; i < length; i++) {
                if (iDevices[i] != null) {
                    //					if(iDevices[i].getNetWorkState() == EIOT_NETWORK_TYPE.EIOT_NETWORK_TYPE_LOCAL && aNetWork != EIOT_NETWORK_TYPE.EIOT_NETWORK_TYPE_LOCAL){
                    //						continue;
                    //					}

                    boolean tMatch = true;

                    address = iDevices[i].getRegisterAddress();
                    size = iDevices[i].getRegisterSize();
                    int regLen = address + size;

                    if (regLen <= aRegLen && regLen > aAddress) {

                        address = aAddress > address ? aAddress : address;

                        tStartPos = Math.abs(aAddress - address);

                        if (aSize <= size) {
                            size = aSize;
                        } else {
                            if (aSize / 2 != size) {
                                tMatch = false;
                            } else {
                                size = aSize / 2;
                            }

                            if (aAddress == 0) {
                                tMatch = true;
                            }

                        }

                    } else {
                        if (aAddress >= address && aAddress < regLen) {
                            tStartPos = 0;
                            size = aSize;
                            tMatch = true;
                        } else {
                            tMatch = false;
                        }

                        address = aAddress > address ? aAddress : address;
                    }

                    if (tMatch) {
                        iDevices[i].updateRegister(address, size, aData, tStartPos, aNetWork, time);
                    }

                }

            }
            //====================modify end==================================

        }
    }

    public int getDeviceCount() {
        if (iDevices != null) {
            return iDevices.length;
        }

        return 0;
    }

    public IOTDevice getDevice(int aIndex) {
        if (iDevices != null && aIndex >= 0 && aIndex < iDevices.length) {
            return iDevices[aIndex];
        }

        return null;
    }

    @Override
    public boolean setAuthState(int aNetWork, int aNewState) {
        if (super.setAuthState(aNetWork, aNewState) && iDevices != null) {
            int length = iDevices.length;
            for (int i = 0; i < length; i++) {
                if (iDevices[i].getID() == getID()) {
                    iDevices[i].setAuthState(aNetWork, aNewState);
                }
            }
            return true;
        }
        return false;
    }


    @Override
    public String getName() {
//        if (super.getName() == null) {
            StringBuilder tName = new StringBuilder();

            if (iDevices != null) {
                int length = iDevices.length;
                for (int i = 0; i < length; i++) {
                    if (i > 0) {
                        tName.append(",");
                    }

                    tName.append(iDevices[i].getName());
                }
            }

            return tName.toString();
//        }

//        return super.getName();
    }

    @Override
    public String getStateID() {
        return "*";
    }

    @Override
    public String[] getStateIDs() {
        return null;
    }

    @Override
    public boolean setOnline(boolean aOnline) {
        if (super.setOnline(aOnline)) {
            if (iDevices != null) {
                int length = iDevices.length;
                for (int i = 0; i < length; i++) {
                    iDevices[i].setOnline(aOnline);
                }
            }

            return true;
        }

        return false;
    }

    public IOTDevice[] getDevices() {
        return iDevices;
    }

}
